package com.example.demo.controller;

import com.example.demo.pojo.Emp;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.Result;
import com.example.demo.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@RequestMapping("/emps")
@RestController // @Controller + @ResponseBody
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping
    public Result selectEmpsByCondition(String name, Byte gender,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                                        @RequestParam(defaultValue = "1") int page,
                                        @RequestParam(defaultValue = "10") int pageSize){
        PageBean pb = empService.selectEmpsByCondition(name,gender,begin,end,page,pageSize);
        if(pb==null)
        {
            return  Result.error("出现错误");
        }
        return Result.success(pb);
    }


    @DeleteMapping("/{ids}")
    public Result deleteEmps(@PathVariable Integer[] ids){
         List<Emp> list = empService.deleteEmpsById(ids);
        if(list==null)
        {
            return  Result.error("出现错误");
        }
         return Result.success(list);
    }


    @PostMapping()
    public Result insertEmps(@RequestBody Emp emp){
        int row=empService.insertEmp(emp);
        if(row!=1)
        {
            return Result.error("出现错误");
        }
        return Result.success(row);
    }


    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id)//将url地址中的参数获取到并且绑定id
    {
        Emp emp=empService.selectById(id);
        if(emp==null)
        {
            return  Result.error("出现错误");
        }
        return Result.success(emp);
    }

    @PutMapping()
    public Result updateEmps(@RequestBody Emp emp)
    {
        int row=empService.updateEmps(emp);
        return Result.success(row);
    }

}
